package com.tom.demo.common.scheduledTasks;

import cn.hutool.core.lang.Snowflake;
import com.tom.demo.common.entity.StockMarket;
import com.tom.demo.common.entity.StockMarketAsync;
import com.tom.demo.mapper.StockMarketAsyncMapper;
import com.tom.demo.mapper.StockMarketMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class SynchronousStockMarket {


    @Resource
    private StockMarketMapper stockMarketMapper;

    @Resource
    private StockMarketAsyncMapper stockMarketAsyncMapper;

    @Resource
    private Snowflake snowflake;

    private static final int limit = 100; // 分批大小
    private static long offset = 0;


    //@Scheduled(cron = "0 0 0 * * ?") //每天0点执行一次
    //@Scheduled(fixedRate = 1000 * 60 * 60 * 24)
    public void stockMarketTask() {
        log.info("stockMarketTask<=========>定时任务执行,开始同步postgresql数据到mysql数据库中");
        //批量读取，每次读取100条
        while (true){
            List<StockMarket> stockMarketkList =  stockMarketMapper.batchReading(limit,offset);
            if(stockMarketkList == null || stockMarketkList.isEmpty()){
                break;
            }
            stockMarketkList.forEach(stockMarket -> {
                StockMarketAsync stockMarketAsync = new StockMarketAsync();
                //1:设置id
                stockMarketAsync.setId(snowflake.nextIdStr());
                //2:设置股票代码
                stockMarketAsync.setStockCode(stockMarket.getStockCode());
                //3:设置公司名称
                stockMarketAsync.setCompanyName(stockMarket.getCompanyName());
                //4:设置市值
                stockMarketAsync.setMarketCap(stockMarket.getMarketCap());
                //5:设置开盘价
                stockMarketAsync.setOpenPrice(stockMarket.getOpenPrice());
                //6:设置收盘价
                stockMarketAsync.setClosePrice(stockMarket.getClosePrice());
                //7:设置最高价
                stockMarketAsync.setHighPrice(stockMarket.getHighPrice());
                //8:设置最低价
                stockMarketAsync.setLowPrice(stockMarket.getLowPrice());
                //9:设置成交量
                stockMarketAsync.setVolume(stockMarket.getVolume());
                //10:设置交易日期
                stockMarketAsync.setTradeDate(stockMarket.getTradeDate());
                //11:设置是否活跃
                stockMarketAsync.setIsActive(stockMarket.getIsActive()?1:0);
                //12:设置市盈率
                stockMarketAsync.setPeRatio(stockMarket.getPeRatio());
                //13:设置股息收益率
                stockMarketAsync.setDividendYield(stockMarket.getDividendYield());
                //14:设置最后更新时间
                stockMarketAsync.setLastUpdated(stockMarket.getLastUpdated().toLocalDateTime());
                //15:设置货币种类
                stockMarketAsync.setCurrency(stockMarket.getCurrency());
                //16:设置所属行业
                stockMarketAsync.setSector(stockMarket.getSector());
                //17:设置员工数量
                stockMarketAsync.setEmployeesCount(stockMarket.getEmployeesCount());
                //18设置总部位置
                stockMarketAsync.setHeadquartersLocation(stockMarket.getHeadquartersLocation());
                stockMarketAsyncMapper.insert(stockMarketAsync);
            });
            offset += limit; // 更新偏移量
        }
        offset = 0; // 重置偏移量以备下次运行
        log.info("stockMarketTask<=========>定时任务执行完成，已经将postgresql数据库中的数据同步到了mysql中!!!");
    }

    /**
     * 该定时任务会走 ScheduleTaskConfig配置中的线程池
     */
    //@Scheduled(fixedRate = 5000)//5秒执行一次
    public void testScheduledTask(){
        log.info("testScheduledTask(方法执行)");
    }

    /**
     * 该定时任务走的是 Bean的名称是:taskExecutor
     * 如果我们没有配置Bean的名称是taskExecutor的时候，走SimpleAsyncTaskExecutor
     * 但是强烈建议:不要在定任务上面使用 @Async,我们只需要配置好:ThreadPoolTaskScheduler的线程池就好，就可以多任务并行执行
     */
    //@Async
    //@Scheduled(fixedRate = 5000)//5秒执行一次
    public void testScheduledTask2(){
        log.info("testScheduledTask2(方法执行)");
    }

    /**
     * 该定时任务会走我们配置的线程池:springAsyncTaskExecutor
     * 但是强烈建议:不要在定任务上面使用 @Async,我们只需要配置好:ThreadPoolTaskScheduler的线程池就好，就可以多任务并行执行
     */
    //@Async(value = "springAsyncTaskExecutor")
    //@Scheduled(fixedRate = 5000)//5秒执行一次
    public void testScheduledTask3(){
        log.info("testScheduledTask3(方法执行)");
    }
}
